<?php

namespace App\Admin\Controllers;

use App\Models\VipWelfareDetail;
use App\Models\VipWelfare;
use App\Models\Product;
use App\Models\Coupon;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class VipWelfareDetailsController extends AdminController
{
    protected $title = 'VIP福利详情';
    
    protected function grid()
    {
        $grid = new Grid(new VipWelfareDetail());
	
	    $grid->disableExport();
	    
	  	$grid->column('welfare_id', 'vip福利')->display(function () {
	        return isset($this->welfare) ? "{$this->welfare->id}: {$this->welfare->name}" : '';
        });
	  	$grid->column('type', '福利类型')->select(config('site.vip.types'));

	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
        return $grid;
    }
    
    protected function detail($id)
    {
        $show = new Show(VipWelfareDetail::findOrFail($id));



        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new VipWelfareDetail());
	    $form->select('welfare_id','福利')->options(VipWelfare::all()->pluck('name', 'id'));
	    $this->formCategory($form);
        return $form;
    }

     protected function formCategory(Form $form)
    {
	    $form->select('type', '福利类型')
		    ->options(config('site.vip.types'))
		     ->when('phone', function (Form $form) {

                  $form->text('phone_bill_name', '话费充值标题');
                  $form->currency('cost', '话费充值金额')->symbol('￥')->default(0);

            })->when('product', function (Form $form) {

                   $form->hasMany('welfareProducts', "商品", function ($form) {
				    	$form->select('product_id', '商品')
				    		->options(Product::all()->pluck('title', 'id'))
						    ->rules('required');
				    });

            })->when('coupon', function (Form $form) {

                  $form->hasMany('welfareCoupons', "优惠卷", function ($form) {
				    	$form->select('coupon_id', '优惠卷')
				    		->options(Coupon::all()->pluck('name', 'id'))
						    ->rules('required');
						$form->number('number', '数量')->min(1)->default(1);
				    });

            })->when('drink', function (Form $form) {

                $form->select('product_id', '商品ID')
				    		->options(Product::all()->where('is_drink',true)->pluck('title', 'id'))
						    ->rules('required');

            });

    }

}
